package com.irisking.ikaisdk.handler;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.irisking.ikaisdk.AbstractServerSDK;
import com.irisking.ikaisdk.dataStruct.BasicJsonRec;
import com.irisking.ikaisdk.dataStruct.BasicJsonResp;
import com.irisking.ikaisdk.dataStruct.DeviceConnInfo;
import com.irisking.ikaisdk.common.JacksonUtil;
import com.irisking.ikaisdk.eventListener.ConnectEventListener;
import com.irisking.ikaisdk.eventListener.EventListener;

import java.util.Date;

/**
 * 设备连接处理
 */
public class deviceconn extends BaseHandler {

    public deviceconn(AbstractServerSDK serverSDK){
        super(serverSDK);

        callBack = new AckCallBack() {
            @Override
            public void call(String sn, long timeStamp, int code, String message ) {
                //System.out.println("deviceconn ack");
                DeviceConnJsonResp resp = new DeviceConnJsonResp();
                resp.setOperation(OperationCode.DEVICE_CONN);
                resp.setTimestamp(timeStamp);
                resp.setCode(code);
                resp.setMessage(message);
                resp.setSn(sn);

                abstractServerSDK.SendAckDataToDevSn(resp, sn);
            }
        };
    }

    @Override
    public void execute( BasicJsonRec rec) {
        DeviceConnJsonRec devRec = (DeviceConnJsonRec) rec;
        // 添加设备到在线列表
        AddDeviceClient(devRec);
        EventListener handler = abstractServerSDK.eventManager.getEventListener(ConnectEventListener.class);
        if (handler!=null){
            // 执行回调通知上层
            ((ConnectEventListener)handler).invoke(devRec.getSn(), devRec.getIp(), devRec.getMac(), devRec.getConnect_time(), devRec.getVersion(), devRec.getServer_id(), devRec.getTimestamp(), callBack);
        }else{
            // 上层为未注册事件，直接返回
            callBack.call(devRec.getSn(), devRec.getTimestamp() , 0, "" );
        }

    }

    /**
     *
     * @param rec
     */
    private void AddDeviceClient(DeviceConnJsonRec rec ){
        DeviceConnInfo devInfo = new DeviceConnInfo();
        devInfo.setSn(rec.getSn());
        devInfo.setIp(rec.getIp());
        devInfo.setMac(rec.getMac());
        devInfo.setConnect_time(rec.getConnect_time());
        devInfo.setVersion(rec.getVersion());
        devInfo.setServer_id(rec.getServer_id());
        devInfo.setDevicekey(rec.getKey());

        abstractServerSDK.putClient(rec.getKey(), devInfo);
    }

    @Override
    public BasicJsonRec deserialize(String json) {
        return  JacksonUtil.getInstance().fromJson(json, DeviceConnJsonRec.class);
    }

    static class DeviceConnJsonResp extends BasicJsonResp {
        private int allow_notice = 0;

        public int getAllow_notice() {
            return allow_notice;
        }

        public void setAllow_notice(int allow_notice) {
            this.allow_notice = allow_notice;
        }
    }

    static class DeviceConnJsonRec extends BasicJsonRec {
        private String ip;           //"xxxxxip",
        private String mac;          // "xxxxxmac",
        @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
        private Date connect_time;   // "2018-04-09 19:12:11",
        private String version;      // "xxxxxversion",
        private String server_id;    // "xxxxxserverid

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public String getMac() {
            return mac;
        }

        public void setMac(String mac) {
            this.mac = mac;
        }

        public Date getConnect_time() {
            return connect_time;
        }

        public void setConnect_time(Date connect_time) {
            this.connect_time = connect_time;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getServer_id() {
            return server_id;
        }

        public void setServer_id(String server_id) {
            this.server_id = server_id;
        }
    }

}
